/*
 * Copyright (c) Created by T C L on 2019.2.26.
 * 内凹矩形
 * Deprecated
 */

package com.egg.ylt.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class ConcaveRectangleView extends View {

    private Paint mPaint;
    private String mContent;

    public ConcaveRectangleView(Context context) {
        super(context);
    }

    public ConcaveRectangleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }


    public ConcaveRectangleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setColor(Color.parseColor("#FFB32D"));
        mPaint.setStrokeWidth(1f);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (!TextUtils.isEmpty(mContent)) {
            Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
            float top = fontMetrics.top;//为基线到字体上边框的距离,即上图中的top
            float bottom = fontMetrics.bottom;//为基线到字体下边框的距离,即上图中的bottom
            int baseLineY = (int) (getWidth() / 2 - top / 2 - bottom / 2);//基线中间点的y轴计算公式
            canvas.drawText(mContent, getWidth() / 2, baseLineY, mPaint);
        }

        // 上面一条线
        canvas.drawLine(0, 0, getWidth(), 0, mPaint);

        canvas.drawCircle(0, (float) getHeight() / 2, (float) getHeight() / 3, mPaint);

        canvas.drawCircle(getWidth(), (float) getHeight() / 2, (float) getHeight() / 3, mPaint);

        //下面一条线
        canvas.drawLine(0, getHeight(), getWidth(), getHeight(), mPaint);
    }

    public void setContent(String content) {
        this.mContent = content;
        postInvalidate();
    }
}
